package top.raxidin.user.controller;


import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.raxidin.common.vo.Result;
import top.raxidin.user.entity.User;
import top.raxidin.user.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;


//    @RequestMapping("/login")
    @PostMapping("/login")
    public Result login(User param, @RequestParam("captcha") String captcha, HttpSession session, HttpServletRequest request){
        //验证码判断
         if(!CaptchaUtil.ver(captcha,request)){
             return Result.fail("验证码错误");
         }

        User user = userService.login(param);
        if(user!=null){
            //登录成功
            user.setPassword(null);
            session.setAttribute("userInfo",user);
            return Result.success();
        }
        //登录失败
        return Result.fail("用户名或密码错误");
    }


}
